//
//  WCNTabBarController.m
//  WCN
//
//  Created by 杨鑫 on 15/5/29.
//  Copyright (c) 2015年 ShanghaiCize Trade And Business Co., Ltd. All rights reserved.
//

#import "HELTabBarVC.h"
#import "WSJTabarStaticData.h"
#import "SQTabbarControllerAnimatedTransitioning.h"
#import "HELHomeVC.h"
#import "HELMeVC.h"
#import "HELNavigationController.h"
#import "BTLoginVC.h"
#import "HELMeVC.h"
#import "HELConsultVC.h"
#import "BTLoginVC.h"
#import "HELBindPhoneVC.h"
#import "JCAlertView.h"
//----------
//两次提示的默认间隔
//static const CGFloat kDefaultPlaySoundInterval = 3.0;
static NSString *kMessageType = @"MessageType";
static NSString *kConversationChatter = @"ConversationChatter";
static NSString *kGroupName = @"GroupName";
@interface HELTabBarVC () <UITabBarControllerDelegate>

    

@property (strong, nonatomic) NSDate *lastPlaySoundDate;


@end

@implementation HELTabBarVC

- (void)viewDidLoad {
    [super viewDidLoad];

    self.delegate = self;
    HELHomeVC *helHomeVC =[HELHomeVC new];
    helHomeVC.title = @"首页";
    HELMeVC *personVC = [[HELMeVC alloc] init];
    personVC.title = @"我";
    HELConsultVC *consultVC =[HELConsultVC new];
    consultVC.title = @"资讯";
    
    HELNavigationController *navHome =[[HELNavigationController alloc] initWithRootViewController:helHomeVC];
    HELNavigationController *consultNav =[[HELNavigationController alloc] initWithRootViewController:consultVC];
    HELNavigationController *navperson =[[HELNavigationController alloc] initWithRootViewController:personVC];
    
    
    self.viewControllers = @[navHome, consultNav,navperson];
    
    navHome.tabBarItem.title = @"首页";
    
    navHome.tabBarItem.image = [[UIImage imageNamed:@"课程"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    navHome.tabBarItem.selectedImage = [[UIImage imageNamed:@"课程选中"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
;
    consultNav.tabBarItem.title = @"资讯";
    consultNav.tabBarItem.image = [[UIImage imageNamed:@"资讯"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    consultNav.tabBarItem.selectedImage =[[UIImage imageNamed:@"资讯选中"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
;
    
    navperson.tabBarItem.title = @"我的";
    navperson.tabBarItem.image =  [UIImage imageNamed:@"我的"];
    navperson.tabBarItem.selectedImage = [[UIImage imageNamed:@"我的选中"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    UITabBar *tarBar = self.tabBar;
    tarBar.translucent = NO;
    tarBar.barTintColor = [UIColor whiteColor];
    
    for (UITabBarItem *item in tarBar.items) {
        
        [item setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor grayColor]} forState:UIControlStateNormal];
        [item setTitleTextAttributes:@{NSForegroundColorAttributeName: kMainColor} forState:UIControlStateSelected];
    }
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(logout) name:LogoutNotification object:nil];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginSucess) name:LoginSuccessNotification object:nil];
     NoficationCenter_Obeserve(logout,TokenFaildNotification);
    NoficationCenter_Obeserve(showBindVC,BindPhoneNotification);
    if ([HELAppManager userInfo]) {
        [self loginSucess];
    }
}

- (void)showBindVC{

    
    HELUser *user =[HELAppManager userInfo];
    if (![user.phoneBindStatus integerValue]) {
        
        weakSelf_define;
        dispatch_after(3.0, dispatch_get_main_queue(), ^{
        [JCAlertView showTwoButtonsWithTitle:@"" Message:@"您的找好尚未验证绑定手机,有可能收不到验证码,是否马上去验证?" ButtonType:JCAlertViewButtonTypeCancel ButtonTitle:@"暂不" Click:nil ButtonType:JCAlertViewButtonTypeMainColor ButtonTitle:@"马上验证" Click:^{
           
                HELNavigationController *nv =[[HELNavigationController alloc] initWithRootViewController:[HELBindPhoneVC new]];
            
                [weakSelf presentViewController:nv animated:YES completion:nil];
          
        }];
    });
      
      
    }
   
    
}

//- (void)viewWillAppear:(BOOL)animated{
//
//    
//}

- (void)logout{
   
    //删除用户信息
    [HELAppManager removeUserInfo];
    BTLoginVC *vc =[BTLoginVC new];
    HELNavigationController *nav =[[HELNavigationController alloc] initWithRootViewController:vc];
    HELNavigationController *currentNav = self.viewControllers[self.selectedIndex];
    [currentNav presentViewController:nav animated:YES completion:nil];
}
- (void)loginSucess{
    //重新初始化数据库/切换
    [[RLMCourseManager manager] configWithUserAccout:[HELAppManager userInfo].mobileNumber];
}

#pragma mark - Setter

- (void)setSelectedIndex:(NSUInteger)selectedIndex
{
    [super setSelectedIndex:selectedIndex];
    _tab_selectedIndex = selectedIndex;
    
    [self tabBarController:self didSelectViewController:self.viewControllers[selectedIndex]];
}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    
//    [item setTitleTextAttributes:@{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Bold" size:10.0f],
//                                                        NSForegroundColorAttributeName : [UIColor purpleColor]
//                                                        } forState:UIControlStateSelected];

}
#pragma mark - UITabBarControllerDelegate
// 当前控制的导航栏项元素
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    self.navigationItem.titleView = viewController.navigationItem.titleView;
    self.navigationItem.leftBarButtonItems = viewController.navigationItem.leftBarButtonItems;
    self.navigationItem.rightBarButtonItems = viewController.navigationItem.rightBarButtonItems;
}
- (void)showBadageOnItem:(NSInteger)index
{
    
    UITabBarItem *item =  self.tabBar.items[index];
    item.badgeValue  =@"new";
    
}


- (id <UIViewControllerAnimatedTransitioning>)tabBarController:(UITabBarController *)tabBarController animationControllerForTransitionFromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC {
    
    _tab_preIndex = [self.viewControllers indexOfObject:fromVC];
    _tab_selectedIndex =[self.viewControllers indexOfObject:toVC];
    
    return [SQTabbarControllerAnimatedTransitioning new];
}


@end
